﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity.Validation;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Datos
{
    [MetadataType(typeof(DetalleVentaMetaData))]
    public partial class DetalleVenta {
        Repository<ArqParcial1Entities> repositorio = new Repository<ArqParcial1Entities>();
        ArqParcial1Entities context = new ArqParcial1Entities();

        public IEnumerable<DetalleVenta> Listar()
        {
            var result = from detalleventa in context.DetalleVentas
                         select detalleventa;
            return result;
        }

        public DetalleVenta Buscar(int iCodigo)
        {
            var result = from detalleventa in context.DetalleVentas
                         where detalleventa.detvenCodigo == iCodigo
                         select detalleventa;
            return result.Count() > 0 ? result.First() : new DetalleVenta();
        }

        public IEnumerable<DetalleVenta> Consultar(int? iCodigo, int? iProducto, DateTime? dtFecha)
        {
            var query = from detalleventa in context.DetalleVentas
                        select detalleventa;
            if (iCodigo == null && iProducto == null && dtFecha == null)
            {
                return new List<DetalleVenta>();
            }
            else
            {
                if (iCodigo != null)
                {
                    query = query.Where(w => w.detvenCodigo == iCodigo);
                }

                if (iProducto != null)
                {
                    query = query.Where(w => w.detvenProducto == iProducto);
                }

                if (dtFecha != null)
                {
                    query = query.Where(w => w.detvenFecha == dtFecha);
                }
                return query;
            }

        }

        public bool Guardar(DetalleVenta detalleventa)
        {
            using (System.Transactions.TransactionScope trn = new System.Transactions.TransactionScope())
            {
                try
                {
                    repositorio.Insert<DetalleVenta>(detalleventa);
                    repositorio.Save();
                    trn.Complete();
                    return true;
                }
                catch (DbEntityValidationException e)
                {
                    foreach (var eve in e.EntityValidationErrors)
                    {
                        Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State);
                        foreach (var ve in eve.ValidationErrors)
                        {
                            Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage);
                        }
                    }
                    return false;

                }
            }
        }
        public bool Actualizar(DetalleVenta detalleventa)
        {
            using (System.Transactions.TransactionScope trn = new System.Transactions.TransactionScope())
            {
                try
                {
                    repositorio.Update<DetalleVenta>(detalleventa);
                    repositorio.Save();
                    trn.Complete();
                    return true;
                }
                catch (DbEntityValidationException e)
                {
                    foreach (var eve in e.EntityValidationErrors)
                    {
                        Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State);
                        foreach (var ve in eve.ValidationErrors)
                        {
                            Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage);
                        }
                    }
                    return false;

                }
            }
        }
        public bool Eliminar(DetalleVenta detalleventa)
        {
            using (System.Transactions.TransactionScope trn = new System.Transactions.TransactionScope())
            {
                try
                {
                    repositorio.Delete<DetalleVenta>(detalleventa);
                    repositorio.Save();
                    trn.Complete();
                    return true;
                }
                catch (DbEntityValidationException e)
                {
                    foreach (var eve in e.EntityValidationErrors)
                    {
                        Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State);
                        foreach (var ve in eve.ValidationErrors)
                        {
                            Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage);
                        }
                    }
                    return false;

                }
            }
        }
    }
    class DetalleVentaMetaData
    {
        [DisplayName("Codigo")]
        public int detvenCodigo { get; set; }
        [DisplayName("Fecha")]
        public System.DateTime detvenFecha { get; set; }
        [DisplayName("Producto")]
        public int detvenProducto { get; set; }
        [DisplayName("Codigo Venta")]
        public int detvenVenta { get; set; }
        [DisplayName("Precio")]
        public decimal detvenPrecio { get; set; }
        [DisplayName("Cantidad")]
        public int detvenCantidad { get; set; }
    }
}
